package com.img2sgf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;



 public class GobanView extends View {
      	        private Paint   mPaint = new Paint();
      	        private float[] lPts;
      	    
      	        private static final float SIZE = 160;
      	        private static final int SEGS = 9;
      	        private float puntoMovibleX;
      	        private float puntoMovibleY;
      	        private BuiltLines bl=new BuiltLines(SIZE,SEGS-1);
      	        
      	        Bitmap bitmap=null;
      	        BitmapDrawable bmd=null;
      	      
      	        public GobanView(Context context, Uri selectedImageUri) {
      	            super(context);    
      	            this.puntoMovibleX=SIZE/2;
      	            this.puntoMovibleY=SIZE/2;
      	          try {
					this.bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(),selectedImageUri);
					this.bmd = new BitmapDrawable(this.bitmap);			           
					                
					
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
      	        }
      	        
      	    
      	        @Override protected void onDraw(Canvas canvas) {
      	        	
      	            this.setBackgroundDrawable(this.bmd);
      	            bl.setCanvas(canvas);
      	            bl.setPaint(new Paint(mPaint));
      	            bl.pintarLineas();
      	        
                    
      	        }
      	        @Override public boolean onTouchEvent(MotionEvent event) {
      	            Point point=new Point(event.getX(),event.getY());     	            
      	            bl.findPoints(point);  
      	            invalidate();
      	            
      	            
      	           int un_pixel, otro_pixel, y_otro_pixel, ultimo_pixel;
      	          
      	           System.out.println("En GobanView");      
      	       
      	         Bitmap bmg = this.toGrayscale(this.bmd.getBitmap());
      	           un_pixel = bmg.getPixel((int)this.bl.getCorner1().getX(),(int)this.bl.getCorner1().getY());
                   otro_pixel =  bmg.getPixel((int)this.bl.getCorner2().getX(),(int)this.bl.getCorner2().getY());
                   y_otro_pixel =  bmg.getPixel((int)this.bl.getCorner3().getX(),(int)this.bl.getCorner3().getY());
                   ultimo_pixel =  bmg.getPixel((int)this.bl.getCorner4().getX(),(int)this.bl.getCorner4().getY());
                 
//                   System.out.println("RGB un_pixel "+Color.red(un_pixel)+"-"+Color.blue(un_pixel)+"-"+Color.green(un_pixel));
//                   System.out.println("RGB otro_pixel "+Color.red(otro_pixel)+"-"+Color.blue(otro_pixel)+"-"+Color.green(otro_pixel));
//                   System.out.println("RGB y_otro_pixel "+Color.red(y_otro_pixel)+"-"+Color.blue(y_otro_pixel)+"-"+Color.green(y_otro_pixel));
//                   System.out.println("RGB ultimo_pixel "+Color.red(ultimo_pixel)+"-"+Color.blue(ultimo_pixel)+"-"+Color.green(ultimo_pixel));
                   System.out.println("RGB un_pixel "+ (int) (Color.red(un_pixel)+Color.blue(un_pixel)+Color.green(un_pixel))/3);
                   System.out.println("RGB otro_pixel "+ (int)(Color.red(otro_pixel)+Color.blue(otro_pixel)+Color.green(otro_pixel))/3);
                   System.out.println("RGB y_otro_pixel "+ (int)(Color.red(y_otro_pixel)+Color.blue(y_otro_pixel)+Color.green(y_otro_pixel))/3);
                   System.out.println("RGB ultimo_pixel "+ (int)(Color.red(ultimo_pixel)+Color.blue(ultimo_pixel)+Color.green(ultimo_pixel))/3);
 
      	            return true;
      	        }
      	        
      	      private Bitmap toGrayscale(Bitmap bmpOriginal)
      	    {        
      	        int width, height;
      	        height = bmpOriginal.getHeight();
      	        width = bmpOriginal.getWidth();    

      	        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
      	        Canvas c = new Canvas(bmpGrayscale);
      	        Paint paint = new Paint();
      	        ColorMatrix cm = new ColorMatrix();
      	        cm.setSaturation(0);
      	        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
      	        paint.setColorFilter(f);
      	        c.drawBitmap(bmpOriginal, 0, 0, paint);
      	        return bmpGrayscale;
      	    }

      	    }
 
// 
// Point.prototype.getColor = function(ctx)
// {
//   try
//   {
//     var c = ctx.getImageData(this.x, this.y, 1, 1);
//   } catch(e) { return {} }
// //  drawPixel(ctx, [0,160,0], this.x, this.y)
//   var r = c.data[0];
//   var g = c.data[1];
//   var b = c.data[2];
//   var max = Math.max(r, g, b);
//   var min = Math.min(r, g, b);
//   return {h: (max - min) / (max + 100) * (256 + 100), l: (r + g + b) / 3};
// }
